//
//  ViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/3/23.
//

#import "ViewController.h"
#import "MeViewController.h"
#import "FunnyBaseViewController.h"
#import "WorldBaseViewController.h"
#import "IMMessageListViewController.h"
#import "SQCLLocationManager.h"
#import "AddFunyPicViewController.h"
#import "VersionPopView.h"
#import "WMDragView.h"
#import "AssistantViewController.h"
#import "SQSocketManager.h"
#import "TicketPopView.h"
@interface ViewController ()
@property (nonatomic , strong) WorldBaseViewController * homeVC;
@property (nonatomic , strong) FunnyBaseViewController * funVC;
@property (nonatomic , strong) IMMessageListViewController * messageVC;
@property (nonatomic , strong) MeViewController * meVC;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [SQSocketManager SQ_SocketManage];
    [self SQ_uiViewForSelf];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if ([UserModel share].isTicket) {
            [[SQCLLocationManager sharedManager]SQ_startUpdatingLocation];
        }
    });
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(SQ_number) name:@"IMMessageModel" object:nil];
}
-(void)SQ_uiViewForSelf{
    [self SQ_Header];
    self.tabBar.backgroundColor = SQHexColor(@"#FFFFFF", 0.4);
    self.tabBar.layer.cornerRadius = 28;
    float height = FootHeight+ 40;
    if (height <= 40) {
        height = 56;
    }
    [self setTabBarFrame:CGRectMake(10, SCREEN_HEIGHT-height, SCREEN_WIDETH-20, 56)
        contentViewFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT)];
    self.tabContentView.loadViewOfChildContollerWhileAppear = YES;
    UIViewController * vc = [[UIViewController alloc]init];
    vc.yp_tabItemImage =  SQGetImage(@"icon-zengjia");
    self.tabContentView.delegate = self;
    self.viewControllers = [NSMutableArray arrayWithObjects:self.homeVC, self.funVC,vc,self.messageVC,self.meVC,nil];
    [self.tabBar setItemContentHorizontalCenterAndMarginTop:14 spacing:0];
    [vc.yp_tabItem setContentHorizontalCenterAndMarginTop:10 spacing:0];

    WS(weakSelf);
    WMDragView *  dragView = [[WMDragView alloc]initWithFrame:CGRectMake(SCREEN_WIDETH-60, SCREEN_HEIGHT-FootHeight-130, 60, 60)];
    dragView.freeRect = CGRectMake(0, TopHeight, SCREEN_WIDETH,SCREEN_HEIGHT-TopHeight-FootHeight-50);
    dragView.imageView.image = SQGetImage(@"logo");
    dragView.layer.cornerRadius = 30;
    dragView.layer.masksToBounds = YES;
    [self.view addSubview:dragView];
    [dragView setClickDragViewBlock:^(WMDragView *dragView) {
        AssistantViewController * vc = [[AssistantViewController alloc]init];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self SQ_number];
}
-(void)SQ_number{
    NSArray * array = [SQFMDBManager SQ_IMAcquire];
    int noReadCount = 0;
    for (IMListModel * model in array) {
        if (model.noReadCount > 0) {
            noReadCount += model.noReadCount;
        }
    }
    self.messageVC.yp_tabItem.badge =  noReadCount;
}

-(void)SQ_Header{
    [HttpTool post:Url6 paramDic:[[NSMutableDictionary alloc]init] successBlock:^(BaseModel * _Nonnull httpModel) {
        [MeModel SQ_setuserModel:httpModel];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        
    }];
    WS(weakSelf);
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"userinfo_switch,good_comment_switch,app_recommended_switch" forKey:@"keys"];
    [HttpTool post:Url58 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        NSArray * array = [[NSArray alloc]initWithArray:httpModel.data];
        for (NSDictionary * switchDic in array) {
            NSString * key = [NSString stringWithFormat:@"%@",[switchDic objectForKey:@"key"]];  
            if ([key isEqualToString:@"good_comment_switch"]) {
                NSString * value = [NSString stringWithFormat:@"%@",[switchDic objectForKey:@"value"]];
                [Manager SQ_SetObject:value FormKey:@"good_comment_switch"];
            } else if ([key isEqualToString:@"userinfo_switch"]) {
                NSString * value = [NSString stringWithFormat:@"%@",[switchDic objectForKey:@"value"]];
                [Manager SQ_SetObject:value FormKey:@"userinfo_switch"];
            }else if ([key isEqualToString:@"app_recommended_switch"]) {
                NSString * value = [NSString stringWithFormat:@"%@",[switchDic objectForKey:@"value"]];
                [Manager SQ_SetObject:value FormKey:@"app_recommended_switch"];
            }
        }
        [TicketPopView SQ_show:weakSelf.view];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
    }];
}

/**
 *  是否能切换到指定index
 */
- (BOOL)tabContentView:(YPTabContentView *)tabConentView shouldSelectTabAtIndex:(NSUInteger)index{
    if (index == 2) {
       AddFunyPicViewController * vc = [[AddFunyPicViewController alloc]init];
       vc.modalPresentationStyle = UIModalPresentationFullScreen;
       [self presentViewController:vc animated:YES completion:nil];
        return NO;
    } else {
        return YES;
    }
}
- (WorldBaseViewController *)homeVC{
    if (!_homeVC) {
        _homeVC = [[WorldBaseViewController alloc]init];
        _homeVC.yp_tabItemImage = SQGetImage(@"图标-未选中-4");
        _homeVC.yp_tabItemSelectedImage = SQGetImage(@"图标-选中-4");
    }
    return _homeVC;
}
- (FunnyBaseViewController *)funVC{
    if (!_funVC) {
        _funVC = [[FunnyBaseViewController alloc]init];
        _funVC.yp_tabItemImage = SQGetImage(@"图标-未选中");
        _funVC.yp_tabItemSelectedImage = SQGetImage(@"图标-选中");
    }
    return _funVC;
}
- (IMMessageListViewController *)messageVC{
    if (!_messageVC) {
        _messageVC = [[IMMessageListViewController alloc]init];
        _messageVC.yp_tabItemImage = SQGetImage(@"图标-未选中-2");
        _messageVC.yp_tabItemSelectedImage = SQGetImage(@"图标-选中-3");
    }
    return _messageVC;
}
- (MeViewController *)meVC{
    if (!_meVC) {
        _meVC = [[MeViewController alloc]init];
        _meVC.yp_tabItemImage = SQGetImage(@"图标-未选中-3");
        _meVC.yp_tabItemSelectedImage = SQGetImage(@"图标-选中-2");
    }
    return _meVC;
}


@end
